/**
 * Created by Administrator on 2017/3/9.
 */
var products = {};// 产品列表{dimension:sale}
var discounts = {};// 任意面额折扣列表{dimension:discount}
var wcPay = null;
/**
 * 提交充值
 */
function submitForm() {
    $("body").trigger("click");
    //验证
    var phoneNum = $("#phoneNumber").val().trim().replace(" ", "").replace(" ", "");
    var dimension = $("#hiddenDimension").val();
    var product = $("#hiddenProduct").val();

    if (!checkPhoneNum(phoneNum)) {
        alert("请输入正确的手机号码！谢谢");
        return;
    }
    if (isNull(dimension)) {
        alert("请选择充值产品！谢谢");
        return;
    }

    $("#submit_form").attr("disabled", "disabled");
    setTimeout(function() {
        $("#submit_form").removeAttr("disabled");
    }, 3000);

    var url = PAY_PATH + "/145";
    var data = {
        "hiddenAccountNo" : phoneNum,
        "hiddenDimension" : dimension,
        "hiddenProduct" : product,
        "userId":userId,
        "memberId":memberId
    }
    toPay(url,data);
}


/**
 * 验证手机号并获取归属地及产品售价信息 TODO 待完善
 */
function checkPhoneNumber(event) {
    var inputVal = $("#phoneNumber").val();
    var product = $("#hiddenProduct").val();

    if (inputVal.length != 0) {
        var oEvent = event || window.event;
        if (oEvent.keyCode != 8) {
            if (inputVal.length == 3 || inputVal.length == 8) {
                $("#phoneNumber").val(inputVal + " ");
            }
        }
        if (oEvent.keyCode == 8) {
            if (inputVal.length == 3 || inputVal.length == 8) {
                inputVal = inputVal.substring(0, inputVal.length - 1);
                $("#phoneNumber").val(inputVal);
            }
        }
        var phoneNum = inputVal.replace(" ", "").replace(" ", "");
        if (!checkPhoneNum(phoneNum)) {
            $("#PhoneInfo").text("");
            $("#otherPrice_div").hide();// 隐藏任意面额的输入框
            return;
        }
        $('#phoneNumber').blur();
        $("body").trigger("click");
        $
            .ajax({
                type : "GET",
                url : OPEN_PATH + "/145/product",
                dataType : 'json', // 返回json格式数据
                data : {
                    "num" : phoneNum,
                    "pt" : product
                }, // 数据
                cache : false,
                error : function() {
                    $("#PhoneInfo").text("查询归属地失败");
                    // var str = newNoProItem();
                    $("#PriceTag_Div").empty();
                    // $("#PriceTag_Div").html(str);
                },
                success : function(msg) {
                    // 刷新面额
                    $("#hiddenDimension").val("");
                    // 重新加载数据
                    var result = msg;
                    var local = result.attr;
                    $("#PhoneInfo").html(local);
                    var productsHtml = "";
                    json = eval(result.products);
                    // 是否支持任意面额
                    var customizable = false;

                    if (isNull(json) || json.length == 0) {
                        productsHtml = newNoProItem();
                    } else {
                        for (var i = json.length - 1; i >= 0; i--) {
                            var dimension = json[i].dimension;
                            var sale = json[i].sale;
                            if (json[i].dimension < 0) {
                                customizable = true;
                                //记录区间折扣信息
                                discounts[dimension] = sale / dimension;
                                continue;
                            }
                            products[parseFloat(dimension)] = sale;

                            productsHtml += newProItem(dimension, sale, product);
                        }

                        if (customizable) {// TODO
                            // 支持任意面额
                            productsHtml = "<a href='javascript:;' name='priceTag' class='weui_grid js_grid' onclick='showOtherInput();'><p id='otherPriceP' class='weui_grid_label' >其他面额</p> </a>"
                            + productsHtml;
                        }
                    }
                    $("#PriceTag_Div").html(productsHtml);
                },
                beforeSend : function() {
                    $("#loadingToast").show();
                },
                complete : function() {
                    $("#loadingToast").hide();
                }
            });
    }

}
/**
 * 设置任意面额的售价
 */
function setOtherPrice() {
    var dimension = $("#otherDimension").val();
    $("#hiddenDimension").val("");
    if (dimension == "") {
        $("#sale").text("");
        return;
    }
    if (checkHalfPrice(dimension) || dimension == 0) {
        $("#sale").text("");
        return;
    }
    if (!checkPrice(dimension)) {
        $("#sale").text("无效金额");
        return;
    }
    if (dimension < 0.01 || dimension > 999.99) {
        $("#sale").text("无效金额");
        return;
    }
    getOtherPrice(dimension);
}
/**
 * 根据任意面额计算售价
 *
 * @param dimension
 */
function getOtherPrice(dimension) {
    var price = products[parseFloat(dimension)];
    if (price == null || price == "undefined") {
        // 无面额，计算售价
        var discount = 1;
        if (dimension > 0 && dimension <= 10) {// 0.01-10元
            discount = discounts[-10];
        } else if (dimension > 10 && dimension <= 20) {
            discount = discounts[-20];
        } else if (dimension > 20 && dimension <= 100) {
            discount = discounts[-100];
        } else if (dimension > 100 && dimension <= 500) {
            discount = discounts[-500];
        } else if (dimension > 500 && dimension <= 1000) {
            discount = discounts[-1000];
        }
        price = ceil4Float(dimension * discount, 2);
        if (discount == null || discount == "undefined") {
            price = "未配置该产品";
            $("#sale").text(price);
            return;
        }
    } else {
        price = ceil4Float(price, 2);
    }
    $("#sale").text("售价:" + price + "元");
    $("#hiddenDimension").val(dimension);
}

/**
 * 显示其他面额控件
 */
function showOtherInput() {
    $("a[name='priceTag']").find("p").css({
        "color" : "#000"
    });
    $("#otherPriceP").css({
        "color" : "red"
    });
    $("#otherPrice_div").show();
}